package com.dj.msgtable.controller;

import com.dj.msgtable.commom.PhoneNumber;
import com.dj.msgtable.commom.TimeGet;
import com.dj.msgtable.entity.Custom;
import com.dj.msgtable.entity.User;
import com.dj.msgtable.service.CustomService;
import com.dj.msgtable.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    CustomService customService;
    @Autowired
    UserService userService;

//    @PostMapping("/distribute/part")
//    @ResponseBody
//    public String distribute(@RequestBody User user, HttpServletRequest request) throws IOException {
//
//        String [] users = user.getUserName().split("/");
//        List<Custom> customs = customService.searchCustomNoFollow();
//        Integer usersLength = 0;
//
//        if (customs.size()==0){
//            return "无可分配资源";
//        }
//        for (int i = 0; i < customs.size(); i++) {
//            Custom custom = new Custom();
//            if (usersLength<users.length){
//                custom.setFollowUser(users[usersLength]);
//                custom.setId(customs.get(i).getId());
//
//            }else {
//                usersLength=0;
//                custom.setFollowUser(users[usersLength]);
//                custom.setId(customs.get(i).getId());
//
//            }
//            usersLength++;
//            customService.addFollowUser(custom);
//        }
//
//        return "分配成功";
//    }


    @PostMapping("/distribute/part")
    @ResponseBody
    public String distribute(@RequestBody User user, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
        if (object!=null){


            String [] users = user.getUserName().split("/");
            List<Custom> customs = customService.searchCustomNoFollow();
            Integer usersLength = 0;

            if (customs.size()==0){
                return "无可分配资源";
            }
            for (int i = 0; i < customs.size(); i++) {
                String followUser = userService.searchLessUser(users);
                int temp = 0;
                if (followUser!=null) {
                    Custom custom = new Custom();
                    custom.setFollowUser(followUser);
                    custom.setId(customs.get(i).getId());
                    customService.addFollowUser(custom);
                    User tempUser = new User();
                    tempUser.setId(Integer.valueOf(followUser));

                    tempUser = userService.searchUserById(tempUser);
                    Integer daily = tempUser.getDailyNum()+1;
                    tempUser.setDailyNum(daily);
                    userService.addUserDailyNum(tempUser);
                    temp++;
                }else {
                    if (temp==0){
                        return "所选人员已分配上限";
                    }
                    return "部分分配成功，无可分配人员";
                }

            }
            return "分配成功";

        }
        return null;
    }

    @PostMapping( "/feedback/selflist")
    @ResponseBody
    public List<Custom> searchUserList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
        if (object!=null){
            User user = (User) object;
            List<Custom> customList =new ArrayList<>();
            List<Custom> customs = customService.searchSelfList(user);
            System.out.println(customs.toString());
            for (int i = 0; i < customs.size(); i++) {
                Custom custom = customs.get(i);


//                Custom custom = new Custom();
//                custom.setId(customs.get(i).getId());
//                custom.setRealName(customs.get(i).getRealName());
//                custom.setPhone(customs.get(i).getPhone());
//                custom.setKeyword(customs.get(i).getKeyword());
//                custom.setArea(customs.get(i).getArea());
//                custom.setArea(customs.get(i).getIsable());
//                System.out.println(custom.toString());
                customList.add(custom);
//                System.out.println(customs.get(i).toString());
            }
            return customList;
        }
        return null;
    }







    //需判断是否是他的资源
    @PostMapping("/feedback/rebackMsg")
    @ResponseBody
    public String rebackMsg(@RequestBody Custom custom, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
        if (object!=null) {
            customService.changeState(custom);
            return "修改成功";
        }
        return null;
    }



    @PostMapping( "/search/searchlist")
    @ResponseBody
    public List<Custom> searchList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
        if (object!=null){
            User sessionuser = (User) object;
            if ("0".equals(sessionuser.getRank())||"1".equals(sessionuser.getRank())) {
                List<Custom> listV= customService.searchAllCustom();
                return listV;
            }
        }
        return null;
    }

//
    @PostMapping( "/feedback/searchone")
    @ResponseBody
    public Custom searchsearchone(@RequestBody Custom custom,HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");

//        Object object = 1 ;
        if (object!=null){
            Custom returnC = customService.searchOne(custom);
            return returnC;
        }
        return null;
    }


    @PostMapping("/formsubmit/addCustombyhand")
    @ResponseBody
    public String addCustombyhand(@RequestBody Custom custom, HttpServletRequest request) throws IOException {
//        customService.changeState(custom);
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");

        if (object!=null) {
            User sessionuser = (User) object;

            if (PhoneNumber.isMobileNO(custom.getPhone())) {
                if (StringUtils.isEmpty(customService.searchCustombyPhone(custom))) {

                    custom.setInputTime(TimeGet.nowTimeSteing());
                    custom.setStatus("新入库");

                    if ("2".equals(sessionuser.getRank())) {
                        custom.setFollowUser(String.valueOf(sessionuser.getId()));
                        customService.addCustomHavef(custom);
                    }else {
                        customService.addCustomNof(custom);
                    }

                } else {return "手机号已存在";}
            }else {return "手机号错误";}

        }

        return "增加成功";
    }


//
    @PostMapping("/distribute/partone")
    @ResponseBody
    public String changeFollow(@RequestBody Custom custom, HttpServletRequest request) throws IOException {
         customService.addFollowUser(custom);
        return "修改成功";
    }


//
    //查询未分派资源数量
    @PostMapping("/distribute/ResourcesNumber")
    @ResponseBody
    public String ResourcesNumber(HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        Object object = session.getAttribute("user");

        if (object != null) {

            List<Custom> customs = customService.searchCustomNoFollow();
            if (customs.size() == 0) {
                return "无可分配资源";
            } else {
                return String.valueOf(customs.size());
            }
        }
        return "请登录";
    }
    @PostMapping("/feedback/rebackMsg2")
    @ResponseBody
    public String rebackMsg2(@RequestBody Custom custom, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
        if (object!=null) {
            customService.changeState2(custom);
            return "修改成功";
        }
        return null;
    }
}
